package cn.zn.controller.system;


import cn.zn.base.annotation.Log;
import cn.zn.base.bean.dto.ResultData;
import cn.zn.base.common.enums.BusinessType;
import cn.zn.base.common.utils.ResultUtil;
import cn.zn.dict.entity.SysDictValue;
import cn.zn.dict.service.SysDictValueService;
import cn.zn.dict.vo.SysDictValueEditVo;
import cn.zn.dict.vo.SysDictValuePageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zn-admin
 * @since 2021-01-28
 */
@RestController
@RequestMapping("/system/dict/value")
public class SysDictValueController {

    @Autowired
    private SysDictValueService dictValueService;

    @PreAuthorize("@ss.hasPermi('system:dict:list')")
    @GetMapping("/list")
    public ResultData<Object> list(SysDictValuePageVo vo) {
        Object list = dictValueService.selectDictValueList(vo);
        return ResultUtil.success(list);
    }

    /**
     * 查询字典数据详细
     */
    @PreAuthorize("@ss.hasPermi('system:dict:info')")
    @GetMapping(value = "/{id}")
    public ResultData<SysDictValue> getInfo(@PathVariable String id) {
        return ResultUtil.success(dictValueService.getById(id));
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @GetMapping(value = "/type/{dictType}")
    public ResultData<List<SysDictValue>> dictType(@PathVariable String dictType) {
        List<SysDictValue> data = dictValueService.selectDictValueByType(dictType);
        return ResultUtil.success(data);
    }

    /**
     * 新增字典类型
     */
    @PreAuthorize("@ss.hasPermi('system:dict:add')")
    @Log(title = "字典数据", businessType = BusinessType.INSERT)
    @PostMapping
    public ResultData add(@RequestBody @Validated SysDictValueEditVo vo) {
        dictValueService.insertDictValue(vo);
        return ResultUtil.success();
    }

    /**
     * 修改保存字典类型
     */
    @PreAuthorize("@ss.hasPermi('system:dict:edit')")
    @Log(title = "字典数据", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/{id}")
    public ResultData edit(@PathVariable String id, @RequestBody @Validated SysDictValueEditVo vo) {
        dictValueService.updateDictValue(id, vo);
        return ResultUtil.success();
    }

    /**
     * 删除字典类型
     */
    @PreAuthorize("@ss.hasPermi('system:dict:remove')")
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public ResultData remove(@PathVariable String[] ids) {
        dictValueService.deleteDictValueByIds(ids);
        return ResultUtil.success();
    }

}
